package com.correction.servlet;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;

public class ExecuteGenerique {

	private void writeError(Process process2) {
		BufferedReader reader = new BufferedReader(new InputStreamReader(
				process2.getErrorStream()));
		String line = "";
		try {
			while ((line = reader.readLine()) != null) {
				System.out.println("Error: " + line);
			}
			reader.close();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	private void writeFileUpload(Process process2, String path)
			throws IOException {
		BufferedReader reader = new BufferedReader(new InputStreamReader(
				process2.getInputStream()));
		String line = "";
		String textFile = "";
		try {
			while ((line = reader.readLine()) != null) {
				textFile = textFile + line;
			}
			FileWriter fw = new FileWriter(path + "\\File-Log.txt");
			BufferedWriter bw = new BufferedWriter(fw);
			bw.write(textFile);
			bw.close();
		} finally {
			reader.close();
		}
	}

	public void executeWithDirection(final String path, String fileName,
			String direction) {
		System.out.println("Path :" + path + " FileName :" + fileName + " Direction :" + direction);
		Runtime runtime = Runtime.getRuntime();
		try {
			final Process process1 = runtime.exec("javac -d " + path + " "
					+ path + File.separator + fileName);
			if (process1.waitFor() == 0) {
				String[] parts = fileName.split("\\.");
				final Process process2 = runtime.exec("java -cp " + path + " "
						+ direction + "." + parts[0]);
				// Consommation de la sortie standard de l'application externe
				// dans un Thread separe
				new Thread() {
					public void run() {
						try {
							writeFileUpload(process2, path);
						} catch (IOException ioe) {
							// Consommation de la sortie d'erreur de
							// l'application externe dans un Thread separe
							writeError(process2);
						}
					}
				}.start();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void executeWithoutDirection(final String path, String fileName) {
		System.out.println("Path :" + path + " FileName :" + fileName);
		Runtime runtime = Runtime.getRuntime();
		try {
			final Process process1 = runtime.exec("javac -g " + path
					+ File.separator + fileName);
			if (process1.waitFor() == 0) {
				String[] parts = fileName.split("\\.");
				final Process process2 = runtime.exec("java -cp " + path + " "
						+ parts[0]);
				// Consommation de la sortie standard de l'application externes
				// dans un Thread separe
				if (process2.waitFor() == 0) {
					new Thread() {
						public void run() {
							try {
								writeFileUpload(process2, path);
							} catch (IOException ioe) {
								// Consommation de la sortie d'erreur de
								// l'application externe dans un Thread separe
								writeError(process2);
							}
						}
					}.start();
				} else
					System.out.println("finding");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
